package com.leokok.jts.learning.jts.core.algorithm.distance;

import org.locationtech.jts.algorithm.distance.DistanceToPoint;
import org.locationtech.jts.algorithm.distance.PointPairDistance;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

/**
 * 点到geometry的最近欧几里得距离计算
 * 同时也返回最近的距离点
 */
public class DistanceToPointTest {

    public static void main(String[] args) throws ParseException {

        WKTReader reader = new WKTReader();
        Geometry line2 = reader.read("POLYGON ((520 320, 511 312, 496 301, 480 288, 470 280, 457 268, 449 257, 443 248, 440 236, 438 226, 438 214, 438 201, 442 188, 452 175, 464 162, 473 157, 484 152, 495 147, 507 143, 521 140, 537 138, 553 135, 613 131, 629 131, 651 131, 668 131, 684 131, 698 131, 711 131, 724 133, 735 135, 747 138, 757 142, 773 149, 785 157, 794 163, 806 175, 813 185, 818 194, 820 207, 820 218, 820 230, 815 244, 803 259, 793 267, 784 272, 771 278, 755 283, 736 290, 711 296, 687 302, 663 308, 636 314, 617 320, 601 323, 589 327, 577 330, 567 332, 553 337, 542 342, 532 346, 522 348, 512 350, 510 350, 520 320))");
        Coordinate c = new Coordinate(680, 320);
        PointPairDistance ppd = new PointPairDistance();
        DistanceToPoint.computeDistance(line2, c, ppd);

        //返回垂线
        System.out.println(ppd.toString());

        System.out.println(ppd.getDistance());
        for (Coordinate cc : ppd.getCoordinates()) {
            System.out.println(cc);
        }

    }
}
